﻿Imports System.Web
Imports Comms.PushUpgrade.Interface

Public Class CUpgradeClient_Config : Inherits CClient_Config

#Region "Constants"
    Public Const COMMON_PREFIX As String = "Upgrade."

    Public Const DEFAULT_HOSTNAME As String = "admin.controltrackonline.com"
	Public Const DEFAULT_USE_SSL As Boolean = True
	Public Const DEFAULT_IGNORES As String = CFolderHash.IGNORE_EXTENSIONS_AS_STRING & ",.xml,.pdb,.refresh,.pfx,.cer,.log"


	Private Const KEY_AppId As String = "AppId"
	Private Const KEY_InstanceId As String = "InstanceId"
	Private Const KEY_InstanceHostName As String = "InstanceHostName"
	Private Const KEY_ClientName As String = "ClientName"
	Private Const KEY_ClientSuffix As String = "ClientSuffix"
	Private Const KEY_IsDevMachine As String = "IsDevMachine"
    Private Const KEY_ExtensionsToIgnore As String = "ExtensionsToIgnore"

    Private Shared PREFIXED_AppId As String = COMMON_PREFIX & KEY_AppId
	Private Shared PREFIXED_InstanceId As String = COMMON_PREFIX & KEY_InstanceId
	Private Shared PREFIXED_InstanceHostName As String = COMMON_PREFIX & KEY_InstanceHostName
	Private Shared PREFIXED_ClientName As String = COMMON_PREFIX & KEY_ClientName
	Private Shared PREFIXED_ClientSuffix As String = COMMON_PREFIX & KEY_ClientSuffix
	Private Shared PREFIXED_IsDevMachine As String = COMMON_PREFIX & KEY_IsDevMachine
    Private Shared PREFIXED_ExtensionsToIgnore As String = COMMON_PREFIX & KEY_ExtensionsToIgnore
#End Region

	Public ReadOnly Property GetId As CIdentity
		Get
			Return New CIdentity(Me.InstanceId, Me.AppId, Me.ClientName, Me.ClientSuffix, Me.HostName, CDataSrc.Default.ConnectionString)
		End Get
	End Property

#Region "Overrides (Prefix, Defaults)"
	Protected Overrides ReadOnly Property KEY_CommonPrefix As String
        Get
            Return COMMON_PREFIX
        End Get
    End Property
    Protected Overrides ReadOnly Property DefaultHostName As String
        Get
            Return DEFAULT_HOSTNAME
        End Get
    End Property
    Protected Overrides ReadOnly Property DefaultUseSsl As String
        Get
            Return DEFAULT_USE_SSL
        End Get
    End Property

	Protected ReadOnly Property MyHostName As String
		Get
			If IsNothing(HttpContext.Current) Then Return String.Empty
			Return HttpContext.Current.Request.Url.Host
		End Get
	End Property
	Protected ReadOnly Property DefaultIsDevMachine As Boolean
        Get
            Return My.Computer.Name.ToUpper = "DESKTOP-KJNAST7" OrElse My.Computer.Name.ToLower = "jeremy-pc"
        End Get
    End Property
#End Region

#Region "Config settings (write first time)"
    Public Shadows Function WriteConfigSetting_Password(Optional value As String = "somelongpassword", Optional overwrite As Boolean = False) As Boolean
        Return MyBase.WriteConfigSetting_Password(value, overwrite)
    End Function
    Public Shadows Function WriteConfigSetting_HostName(Optional value As String = DEFAULT_HOSTNAME, Optional overwrite As Boolean = False) As Boolean 'expose the default value via optional param
        Return MyBase.WriteConfigSetting_HostName(value, overwrite)
    End Function
    Public Shadows Function WriteConfigSetting_UseSsl(Optional value As Boolean = DEFAULT_USE_SSL, Optional overwrite As Boolean = False) As Boolean 'expose the default value via optional param
        Return MyBase.WriteConfigSetting_UseSsl(value, overwrite)
    End Function
    Public Function WriteConfigSetting_ExtensionsToIgnore(Optional value As String = DEFAULT_IGNORES, Optional overwrite As Boolean = True) As Boolean
        Return WriteConfigSetting(KEY_ExtensionsToIgnore, value, overwrite)
    End Function
	Public Function WriteConfigSetting_AppId(appId As Integer, Optional overwrite As Boolean = False) As Boolean
		Return WriteConfigSetting(KEY_AppId, appId, overwrite)
	End Function
	Public Function WriteConfigSetting_ClientName(Optional clientName As String = Nothing, Optional overwrite As Boolean = False) As Boolean
		Return WriteConfigSetting(KEY_ClientName, clientName, overwrite)
	End Function
	Public Function WriteConfigSetting_ClientSuffix(Optional suffix As String = Nothing, Optional overwrite As Boolean = False) As Boolean
		Return WriteConfigSetting(KEY_ClientSuffix, suffix, overwrite)
	End Function
	Public Function WriteConfigSetting_InstanceId(instanceId As Integer, Optional overwrite As Boolean = False) As Boolean
		Return WriteConfigSetting(KEY_InstanceId, instanceId, overwrite)
	End Function
	Public Function WriteConfigSetting_InstanceHostName(hostName As String, Optional overwrite As Boolean = False) As Boolean
		Return WriteConfigSetting(KEY_InstanceHostName, hostName, overwrite)
	End Function


	'New Config settings
	Public ReadOnly Property IsDevMachine As Boolean
		Get
			Return ConfigBool_(PREFIXED_IsDevMachine, KEY_IsDevMachine, DefaultIsDevMachine)
		End Get
	End Property
	Public ReadOnly Property InstanceHostName As String
		Get
			Return Config_(PREFIXED_InstanceHostName, KEY_InstanceHostName, MyHostName)
		End Get
	End Property
	Public ReadOnly Property ClientName As String
		Get
			Return Config_(PREFIXED_ClientName, KEY_ClientName, String.Empty)
		End Get
	End Property
	Public ReadOnly Property ClientSuffix As String
		Get
			Return Config_(PREFIXED_ClientSuffix, KEY_ClientSuffix, String.Empty)
		End Get
	End Property
	Public ReadOnly Property InstanceId As Integer
		Get
			Return ConfigInt_(PREFIXED_InstanceId, KEY_InstanceId, Integer.MinValue)
		End Get
	End Property
	Public ReadOnly Property AppId As Integer
        Get
            Return ConfigInt_(PREFIXED_AppId, KEY_AppId, Integer.MinValue)
        End Get
    End Property
    Public ReadOnly ExtensionsToIgnore_AsArray As String() = CUtilities.StringToListStr(ExtensionsToIgnore).ToArray()
    Public ReadOnly Property ExtensionsToIgnore As String
        Get
            Return Config_(PREFIXED_ExtensionsToIgnore, KEY_ExtensionsToIgnore, DEFAULT_IGNORES)
        End Get
    End Property

    Public ReadOnly Property Identity As CIdentity
        Get
            Dim id As New CIdentity
            id.AppId = AppId
			id.InstanceId = InstanceId
			id.ClientName = ClientName
			id.ClientSuffix = ClientSuffix
			id.HostName = InstanceHostName
			Return id
        End Get
    End Property
#End Region

#Region "Singleton (Shared access to member variables)"
    Private Shared m_shared As New CUpgradeClient_Config
    Public Shared Shadows ReadOnly Property [Shared] As CUpgradeClient_Config
        Get
            Return m_shared
        End Get
    End Property
#End Region

End Class
